package com.zwxbc.funplay.servlet;

import com.google.gson.Gson;
import com.zwxbc.funplay.entity.GameComment;
import com.zwxbc.funplay.service.GameCommentService;
import com.zwxbc.funplay.service.impl.GameCommentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@WebServlet("/comment")
public class CommentServlet extends HttpServlet {
	GameCommentService gameCommentService = new GameCommentServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");

		String action = request.getParameter("action");

		if ("getAll".equals(action)) {
			// 获取所有游戏评论数据
			handleGetList(request, response);
		} else if ("getById".equals(action)) {
			// 获取单个游戏评论数据
			handleGetById(request, response);
		} else {
			// 未知的操作类型
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			response.getWriter().println("未知的操作");
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");

		if ("add".equals(action)) {
			// 添加游戏评论
			try {
				handleAdd(request, response);
			} catch (ParseException | ServletException e) {
				throw new RuntimeException(e);
			}
		} else if ("update".equals(action)) {
			// 更新游戏评论
			try {
				handleUpdate(request, response);
			} catch (ParseException e) {
				throw new RuntimeException(e);
			}
		} else if ("delete".equals(action)) {
			// 删除游戏评论
			handleDelete(request, response);
		} else {
			// 未知的操作类型
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			response.getWriter().println("Unknown action");
		}
	}

	// 添加新游戏评论
	private void handleAdd(HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException, ServletException {
		// 从请求中提取参数
		String gameComment = request.getParameter("comment");
		Long playerId = Long.valueOf(request.getParameter("playerId"));
		Long gameId = Long.valueOf(request.getParameter("gameId"));

		// 创建一个 GameComment 对象
		GameComment newGameComment = new GameComment();
		newGameComment.setComment(gameComment);
		newGameComment.setGameId(gameId);
		newGameComment.setPlayerId(playerId);
		newGameComment.setCommentDate(new Date());

		System.out.println(newGameComment);
		
		// 调用服务方法添加新游戏评论
		boolean success = gameCommentService.addGameComment(newGameComment);
		request.getRequestDispatcher("/view/player/gameInfo.jsp").forward(request, response);
		// 发送响应
//		sendJsonResponse(response, "{\"success\":" + success + "}");
	}

	// 更新游戏评论
	private void handleUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
		// 从请求中提取参数

		GameComment updatedGameComment = new GameComment();


		System.out.println(updatedGameComment);
		// 调用服务方法更新游戏评论
		boolean success = gameCommentService.updateGameComment(updatedGameComment);

		// 发送响应
		sendJsonResponse(response, "{\"success\":" + success + "}");
	}

	// 删除游戏评论
	private void handleDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 从请求中提取参数
		String gameCommentId = request.getParameter("gameCommentId");

		// 调用服务方法删除游戏评论
		boolean success = gameCommentService.deleteGameComment(Long.parseLong(gameCommentId));

		// 发送响应
		sendJsonResponse(response, "{\"success\":" + success + "}");
	}
	
	// 根据ID查询游戏评论
	private void handleGetById(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 从请求中提取参数
		String gameCommentIdParam = request.getParameter("gameCommentId");

		Long gameCommentId = Long.parseLong(gameCommentIdParam);

		// 调用方法根据ID获取游戏评论
		GameComment gameComment = gameCommentService.getGameCommentById(gameCommentId);

		// 将 GameComment 对象转换为 JSON 并发送响应
		String jsonData = new Gson().toJson(gameComment);
		sendJsonResponse(response, jsonData);
	}


	private void handleGetList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<GameComment> gameCommentList = gameCommentService.getGameCommentList();
		// 将 GameComment 对象转换为 JSON 并发送响应
		String jsonData = new Gson().toJson(gameCommentList);
		sendJsonResponse(response, jsonData);
	}


	private void sendJsonResponse(HttpServletResponse response, String jsonData) throws IOException {
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(jsonData);
		out.flush();
	}
}
